package com.eolcum.evofe;

import com.example.evofe.R;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Messenger;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends ActionBarActivity {

	Messenger myService = null;
	private WebView mWebView;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		

		mWebView = (WebView) findViewById(R.id.webview);
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		mWebView.loadUrl("file:///android_asset/index.html");
		mWebView.setWebChromeClient(new WebChromeClient() {
			public void onConsoleMessage(String message, int lineNumber, String sourceID) {
				Log.d("MyApplication", message + " -- From line " + lineNumber + " of " + sourceID);
			}
		});

//		Intent intent = new Intent();
//		intent.setAction("com.eolcum.evo.EVO_ACTION");
//		intent.putExtra("function_name", "getApartment");
//		sendBroadcast(intent);
//
//		intent = new Intent();
//		intent.setAction("com.eolcum.evo.EVO_ACTION");
//		intent.putExtra("function_name", "getLog");
//		intent.putExtra("room_id", 1);
//		sendBroadcast(intent);
	}

	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(receiver, new IntentFilter("com.eolcum.evo.EVO_ACTION"));
//		Intent intent = new Intent();
//		intent.setAction("com.eolcum.evo.EVO_ACTION");
//		intent.putExtra("function_name", "getApartment");
//		sendBroadcast(intent);
//		
//		intent = new Intent();
//		intent.setAction("com.eolcum.evo.EVO_ACTION");
//		intent.putExtra("function_name", "getLog");
//		intent.putExtra("room_id", 1);
//		sendBroadcast(intent);
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(receiver);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction() != null) {
				if (intent.getAction().equals("com.eolcum.evo.EVO_ACTION")) {
					Log.i("Receiver", "Bound to EvoService");
					if ("getApartment".equals(intent.getExtras().getString("function_name"))) {
						Log.i("apartment", intent.getExtras().getString("apartment"));
						mWebView.loadUrl("javascript:addRow('" + intent.getExtras().getString("apartment") + "')");
					}
				}
			}
		}
	};

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container, false);
			return rootView;
		}
	}

}
